package org.tbyrne.motion
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class AtomicSprite extends Sprite
	{
		public function get destinationX():Number{
			return motionX.destination;
		}
		public function set destinationX(to:Number):void{
			motionX.destination = to;
		}
		public function get destinationY():Number{
			return motionY.destination;
		}
		public function set destinationY(to:Number):void{
			motionY.destination = to;
		}
		public function get mass():Number{
			return motionX.mass;
		}
		public function set mass(to:Number):void{
			motionX.mass = motionY.mass = to;
		}
		public function get acceleration():Number{
			return motionX.acceleration;
		}
		public function set acceleration(to:Number):void{
			motionX.acceleration = motionY.acceleration = to;
		}
		public function get decceleration():Number{
			return motionX.decceleration;
		}
		public function set decceleration(to:Number):void{
			motionX.decceleration = motionY.decceleration = to;
		}
		override public function set x(to:Number):void{
			super.x = motionX.value = to;
		}
		override public function get x():Number{
			return super.x;
		}
		override public function set y(to:Number):void{
			super.y = motionY.value = to;
		}
		override public function get y():Number{
			return super.y;
		}
		public function get motionAngle():Number{
			if(!motionX.speed)return (motionY.speed>0?90:180);
			return Math.atan2(motionY.speed,motionX.speed)*(180/Math.PI)+90;
		}
		public function get motionSpeed():Number{
			return Math.sqrt(Math.pow(motionX.speed,2)+Math.pow(motionY.speed,2));
		}
		
		protected var motionX:AtomicMotion;
		protected var motionY:AtomicMotion;
		
		public function AtomicSprite(){
			motionX = new AtomicMotion();
			motionX.value = 0;
			//motionX.rounding = 0.0001;
			motionY = new AtomicMotion();
			motionY.value = 0;
			//motionY.rounding = 1;
			// can't bind because of getter/setter overrides
			motionX.addEventListener(AtomicMotion.VALUE_CHANGE,xChange);
			motionY.addEventListener(AtomicMotion.VALUE_CHANGE,yChange);
		}
		private function xChange(e:Event):void{
			super.x = motionX.value;
		}
		private function yChange(e:Event):void{
			super.y = motionY.value;
		}
	}
}